package com.tao.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.tao.content.domain.dto.BindTeachplanMediaDto;
import com.tao.content.domain.pojo.Teachplan;
import com.tao.content.domain.pojo.TeachplanMedia;
import com.tao.content.mapper.TeachplanMediaMapper;
import com.tao.content.service.ITeachplanMediaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author taohongrun
 * @since 2024-11-01
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements ITeachplanMediaService {

    @Override
    @Transactional //涉及可能产生异常的操作或多数据库操作要加事务
    public void association(BindTeachplanMediaDto bindTeachplanMediaDto){
        //由于规定一个计划只能绑定一个媒资，因此需要先删除再添加
        //不能用saveorupdate，前端不传id
        lambdaUpdate().eq(TeachplanMedia::getTeachplanId,bindTeachplanMediaDto.getTeachplanId()).remove();

        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);

        Teachplan teachplan = Db.getById(bindTeachplanMediaDto.getTeachplanId(), Teachplan.class);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        save(teachplanMedia);
    }
}
